<%@ WebHandler Language="C#" Class="imageManager" %>
/**
 * Created by visual studio2010
 * User: xuheng
 * Date: 12-3-7
 * Time: 下午16:29
 * To change this template use File | Settings | File Templates.
 */
using System;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

public class imageManager : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string[] paths = { "upload/", "upload1/" }; //需要遍历的目录列表，最好使用缩略图地址，否则当网速慢时可能会造成严重的延时
        string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };                //文件允许格式

        string action = context.Server.HtmlEncode(context.Request["action"]);

        if (action == "get")
        {
            String str = String.Empty;
            
            foreach (string path in paths)
            {
                DirectoryInfo info = new DirectoryInfo(context.Server.MapPath(path));

                //目录验证
                if (info.Exists)
                {
                    DirectoryInfo[] infoArr = info.GetDirectories();
                    foreach (DirectoryInfo tmpInfo in infoArr)
                    {
                        foreach (FileInfo fi in tmpInfo.GetFiles())
                        {
                            if (Array.IndexOf(filetype, fi.Extension) != -1)
                            {
                                str += path+"/" + tmpInfo.Name + "/" + fi.Name + "ue_separate_ue";
                            }
                        }
                    }
                }
            }
           
            context.Response.Write(str);
        }
        //Add Start========================================================== 2013-05-12  
        //删除选中的文件  
        string pathDel = string.Empty;                  //最好使用缩略图地址，否则当网速慢时可能会造成严重的延时  
        string fileName = context.Server.HtmlEncode(context.Request["fileName"]);
        bool isDeleted = false;
        if (action == "del")
        {
            try
            {

                String fullPath = String.Empty;
                foreach (string path in paths)
                {
                    pathDel = context.Server.MapPath(path);
                    DirectoryInfo info = new DirectoryInfo(pathDel);

                    //目录验证  
                    if (info.Exists)
                    {
                        //获得C:\...\ueditor\net\upload目录下，以时间命名的目录。如：2013-05-12  
                        DirectoryInfo[] infoArr = info.GetDirectories();
                        foreach (DirectoryInfo tmpInfo in infoArr)
                        {
                            foreach (FileInfo fi in tmpInfo.GetFiles())
                            {
                                //判断是否是指定的图片类型，因为长传的附件和图片在同一个目录  
                                if (Array.IndexOf(filetype, fi.Extension) != -1)
                                {
                                    if (fi.Name.Equals(fileName))
                                    {
                                        fullPath = pathDel + "/" + tmpInfo.Name + "/" + fileName;
                                        File.Delete(fullPath);
                                        isDeleted = true;
                                        break;
                                    }
                                }
                            }
                            //已经删除，往外跳出  
                            if (isDeleted == true)
                                break;
                        }
                    }
                    //已经删除，往外跳出  
                    if (isDeleted == true)
                        break;
                }
                isDeleted = false;
                context.Response.Write("success");
            }
            catch
            {
                context.Response.Write("error");
            }
        }
        //Add End============================================================  2013-05-12 
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}